//
//  lxStringUtility.m
//  toptenforiphone
//
//  Created by 倫星 on 2013/10/07.
//  Copyright (c) 2013年 倫星. All rights reserved.
//

#import "lxStringUtility.h"

@implementation lxStringUtility

+ (NSString *)nil2String:(NSString *)theString {
	NSString *str_tmp;
	if (nil == theString) {
		str_tmp = @"";
	}else {
		str_tmp = theString;
	}
	return str_tmp;
}

+ (NSString *)stringTrim:(NSString *)theString {
    if (nil == theString) {
        return nil;
    }
    
    NSString *str_tmp = [theString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return str_tmp;
}

+ (NSString *)stringClearSpace:(NSString *)theString {
    if (nil == theString) {
        return nil;
    }

    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
    
    NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces];
    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
    NSString *str_tmp = [filteredArray componentsJoinedByString:@" "];
    return str_tmp;
}

+ (BOOL)lastCharCompare:(const NSString *)strSrc strDes:(const NSString *)strDes {
    if (nil == strSrc) {
        return NO;
    }
    
    if (nil == strDes) {
        return NO;
    }
    
    int lenStrSrc = [strSrc length];
    int lenStrDes = [strDes length];
    
    if (lenStrSrc < lenStrDes) {
        return NO;
    }
    
    int index_start = lenStrSrc - lenStrDes;
    NSString *lastStrSrc = [strSrc substringFromIndex:index_start];
    
    BOOL ret = [strDes compare:lastStrSrc options:NSLiteralSearch];
    
    return ret;
}

+ (BOOL)isNil:(NSString *)theString {
    if (nil == theString) {
        return YES;
    }
    if ([theString isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

+ (BOOL)isInt:(NSString *)theString {
    NSScanner* scan = [NSScanner scannerWithString:theString];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

+ (BOOL)isFloat:(NSString *)theString {
    NSScanner* scan = [NSScanner scannerWithString:theString];
    float val;
    return [scan scanFloat:&val] && [scan isAtEnd];
}

+ (BOOL)isDouble:(NSString *)theString {
    NSScanner* scan = [NSScanner scannerWithString:theString];
    double val;
    return [scan scanDouble:&val] && [scan isAtEnd];
}

+ (BOOL)isNum:(NSString *)theString {
    if (nil == theString) {
        return NO;
    }
    BOOL isInt = [lxStringUtility isInt:theString];
    if (YES == isInt) {
        return YES;
    }
    BOOL isFloat = [lxStringUtility isFloat:theString];
    if (YES == isFloat) {
        return YES;
    }
    BOOL isDouble = [lxStringUtility isDouble:theString];
    if (YES == isDouble) {
        return YES;
    }
    return NO;
}

@end
